﻿// <copyright file="CSRData.cs" company="Sinclair Community College">
// Copyright 2010 Sinclair Community College
// </copyright>
// <originalAuthor>Patrick S. Seymour (patrick.seymour@gmail.com)</originalAuthor>

namespace SinclairCC.AppV
{
    using System;

    public class CSRData
    {
        /// <summary>
        /// Time when the volume was created.
        /// </summary>
        /// <remarks>
        /// Time is in UTC format.
        /// </remarks>
        private DateTime creationTime;

        /// <summary>
        /// Number of bytes in the volume label including the null double byte character.
        /// </summary>
        private uint volumeLabelLength;

        /// <summary>
        /// volume name
        /// </summary>
        /// <remarks>
        /// This is a fixed size array of 50 bytes.
        /// </remarks>
        private string volumeLabel;

        /// <summary>
        /// Initializes a new instance of the CSRData class.
        /// </summary>
        public CSRData(System.IO.BinaryReader fileReader)
        {
            // This is the default value for the volume label.
            this.volumeLabel = "AppVirt";

            this.ReadContainer(fileReader);
        }

        /// <summary>
        /// Reads the actual data for this container from the file.
        /// </summary>
        protected void ReadContainer(System.IO.BinaryReader fileReader)
        {
            long creationTimeLong = fileReader.ReadInt64();
            this.creationTime = DateTime.FromFileTimeUtc(creationTimeLong);
            this.volumeLabelLength = fileReader.ReadUInt32();

            byte[] stringBytes;
            System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();
            stringBytes = fileReader.ReadBytes(50);
            if (this.volumeLabelLength <= stringBytes.Length)
            {
                this.volumeLabel = encoding.GetString(stringBytes, 0, (int)volumeLabelLength);
            }
        }

        public DateTime CreationTime
        {
            get { return this.creationTime; }
        }

        public string VolumeLabel
        {
            get { return this.volumeLabel; }
        }
    }
}
